#!/bin/sh
#
# rc Script for vsftpd.
# (c) dangbinghoo@gmail.com.
#     playpenguin.org
#

mdlist="/etc/conf.d/module.lst"
. /etc/init.d/rcfunctions

start()
{	
	rc_do_msg "Auto loading modules and creating device nodes"
	echo
	echo -n "  "
	
	cat $mdlist | while read line;do

		# skip comment line
		if [ -n "`echo $line | grep '^#'`" ];then
			continue
		fi
		
		ko_file=`echo "$line"|awk '{print $1}'`			
		modname=`echo "$line"|awk '{print $2}'`			
		devnode=`echo "$line"|awk '{print $3}'`			
		devsubn=`echo "$line"|awk '{print $4}'`			
		devtype=`echo "$line"|awk '{print $5}'`			

		
		if [ "${ko_file}" != "-" ];then
			if [ -f /lib/modules/`uname -r`/${ko_file} ];then
				/sbin/insmod /lib/modules/`uname -r`/${ko_file} > /dev/null 
			fi
		fi

		if [ "${devnode}" != "-" ];then
		
			if [ -z "${devnode}" ];then
				continue
			fi
			
			devmstn="`cat /proc/devices|grep "$modname"|awk '{print $1}'`"
			
			if [ -f /dev/$devnode ];then
				echo "" > /dev/null	
			else
				/bin/mknod  -m 600 /dev/$devnode $devtype $devmstn $devsubn
			fi
		fi
	done

	rc_status "${DE_msg}"
}


restart()
{
	stop
	start
}

stop()
{
	rc_do_msg "Removing modules and devices nodes "

	cat $mdlist | while read line;do
	
		# skip comment line
		if [ -n "`echo $line | grep '^#'`" ];then
			continue
		fi
		
		ko_file=`echo "$line"|awk '{print $1}'`			
		rmmodname=`echo ${ko_file}|awk -F '/' '{print $NF}'|awk -F '.ko' '{print $1}'`
		
		if [ -n "${rmmodname}" ];then
			/sbin/rmmod ${rmmodname}
		fi
		
		devnode=`echo "$line"|awk '{print $3}'`			
		
		if [ -n "$devnode" ];then
			if [ -n `ls /dev/$devnode|grep "/dev/$devnode"` ];then
				rm -f /dev/$devnode
			fi
		fi
	done

	rc_status "${DE_msg}"
}

#
# main
#

case "$1" in
	"start")
		start;;
	"stop")	
		stop;;
	"restart")
		restart;;
	*)
		echo "Usage: $0 start|stop|restart"
		exit 0
esac

